package is.ru.honn.rumblr.data;

import is.ru.honn.rumblr.domain.AbstractRumblrPost;
import is.ru.honn.rumblr.factory.PostFactory;
import is.ruframework.data.RuData;
import is.ruframework.data.RuDataAccessException;
import is.ruframework.data.RuDuplicateDataException;
import org.springframework.dao.DataIntegrityViolationException;
import org.springframework.jdbc.core.simple.SimpleJdbcInsert;
import org.springframework.jdbc.core.simple.SimpleJdbcTemplate;

import java.util.*;

/**
 * Created by IntelliJ IDEA.
 * User: Notandi
 * Date: 11.10.2011
 * Time: 19:22
 * To change this template use File | Settings | File Templates.
 */
public class PostData extends RuData implements PostDataGateway
{
    //Inserts a new post into the post table along with its details into another table.
    public void addPost(AbstractRumblrPost post, String username)
    {
        List<PostDetail> detailPosts = new ArrayList<PostDetail>();
        SimpleJdbcInsert insertPost = new SimpleJdbcInsert(getDataSource()).withTableName("ru_posts");
        SimpleJdbcInsert insertDetails = new SimpleJdbcInsert(getDataSource()).withTableName("ru_post_details");

        Map<String, Object> postParameters = new HashMap<String, Object>(5);
        Map<String, Object> detailParameters = new HashMap<String, Object>(3);
        //Defines the parameters and tries to insert them into the post table
        try
        {
            postParameters.put("id", post.getId());    //Creates the postParameters
            postParameters.put("url", post.getUrl());
            postParameters.put("dateGmt", post.getDateGmt());
            postParameters.put("type", post.getType());
            postParameters.put("username", username);
            insertPost.execute(postParameters);    //Inserts the postParameters to table

            //Sends and insert for each detailPost
            detailPosts = PostFactory.createPostDetailsFromPost(post);
            for (PostDetail pd: detailPosts)
            {
                detailParameters.put("postid", pd.getPostid());  //Creates the detailParameters
                detailParameters.put("name", pd.getName());
                detailParameters.put("value", pd.getValue());
                insertDetails.execute(detailParameters);  //Insert into the detail table
            }
        }
        catch (DataIntegrityViolationException ex)
        {
            throw new RuDuplicateDataException(ex.getMessage(), ex);
        }

    }

    //This function sends sql query and returns a list of posts of the user specified in in the input parameter
    public Collection getPosts(String username) throws RuDataAccessException
    {
        SimpleJdbcTemplate queryPosts = new SimpleJdbcTemplate(getDataSource());
        Collection<AbstractRumblrPost> posts = queryPosts.query("select * from ru_posts where username = ?",
                                                           new PostRowMapper(), username);
        try
        {
            //Gets the details for each post in the list and calls a postfactory function to merge them into the post.
            for(AbstractRumblrPost post : posts)
            {
                Collection<PostDetail> postDetails = queryPosts.query("select * from ru_post_details where postid = ?",
                                                             new PostDetailMapper(), post.getId());
                post = PostFactory.mergeDetails(post, postDetails);  //Calls a function to add the details into the post
            }
        }
        catch (Exception ex)
        {
              String msg = "Unable to insert PostDetail in mergeDetails";
              throw new RuDataAccessException(msg);
        }

        return posts;
    }

}
